<?php
session_start();
$currPage = "manage";
include("inc_header.php"); ?>

	<div id="content">
		<h2><a href="#">Manage Users</a></h2>
		<div class="clearfix">
	
		<?php
			
			$host="mysql-user.cse.msu.edu"; // Host name
			$username="hewittry"; // Mysql username
			$password="A39777266"; // Mysql password
			$db_name="hewittry"; // Database name
			
			mysql_connect("$host", "$username", "$password")or die("cannot connect");
			mysql_select_db("$db_name")or die("cannot select DB");
			
			if (isset($_REQUEST['id']))
			{	
				$query = "SELECT * FROM AllUsers WHERE LogonID = '" . $_REQUEST['id'] . "'";
				$result = mysql_query($query);
				if (mysql_num_rows($result) == 0)
				{
					die ("Error: that user doesn't exist in the database");
				}
				
				$query = "DELETE FROM AllUsers WHERE LogonID = '" . $_REQUEST['id'] . "'";
				mysql_query($query);
				if (mysql_affected_rows() > 0)
				{
					echo "" . $_REQUEST['id'] . " was successfully deleted!<br /><br />";
				}
				else
				{
					echo "Error";
				}
			}
			
			$query="SELECT * FROM AllUsers WHERE userType > 1";
			$result=mysql_query($query);
			
			$tableRows = "";
			while ($row = mysql_fetch_assoc($result))
			{
				$tableRows .= "<tr>";
				$tableRows .= "<td style='padding: 10px;'>" . $row['LogonID'] . "</td>";
				$tableRows .= "<td style='padding: 10px;'>" . (($row['userType'] == '2') ? "Dealer" : "Regular User") . "</td>";
				$tableRows .= "<td style='padding: 10px;'><a href='deleteUser.php?id=" . $row["LogonID"] . "'>X</a></td>";
				$tableRows .= "</tr>";			
			}
			
			if ($tableRows == "")
			{ 
				echo "<i>Oops! No regular users or dealers are registered on this site. Maybe you should advertise more?";
			}
			else
			{
		?>
			<table cellpadding="20">
				<tr>
					<td><strong>User</strong></td>
					<td><strong>User Level</strong></td>
					<td><strong>Delete</strong></td>
				</tr>
				<?php echo $tableRows; ?>
			</table>
		<?php } ?>		
		</div>
	</div>

<?php include("inc_footer.php"); ?>
